package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.AdminStock;
import com.example.service.AdminStockService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("adminStocks")
public class AdminStockController {
    

    @Autowired
    private AdminStockService adminStockService;

    /**
     * 库存管理员登录
     * @return
     */
    @PostMapping("/loginAdminStock")
    public R login(HttpServletRequest request, @RequestBody AdminStock adminStock){

        LambdaQueryWrapper<AdminStock> lqw= new LambdaQueryWrapper<>();
        lqw.eq(Strings.isNotEmpty(adminStock.getJobNumber()),AdminStock::getJobNumber,adminStock.getJobNumber());

        AdminStock adminStock1 = adminStockService.getOne(lqw);

        if (adminStock1 != null) {
//            request.getSession().setAttribute("adminStock",adminStock1);
            return R.success("登录成功！",adminStock1);

        }else {
            return R.error("登录失败！");
        }
    }

    /**
     * 分页查询所有库存管理员信息
     * @return
     */

    @GetMapping("/getPageAdminStock/{currentPage}/{pageSize}")
    public R getPage(@PathVariable int currentPage,@PathVariable int pageSize,AdminStock adminStock){

        IPage page=new Page(currentPage,pageSize);
        LambdaQueryWrapper<AdminStock> lqw =new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(adminStock.getJobNumber()),AdminStock::getJobNumber,adminStock.getJobNumber());

        IPage page1 = adminStockService.page(page,lqw);


        return R.success("分页查询所有adminStocks成功！",page1);
    }



//    /**
//     * 查询当前库存管理员
//     * @return
//     */
//
//    @GetMapping("/getMyself/{id}")
//
//    public R getOne(@PathVariable int id){
//
//        AdminStock adminStock= adminStockService.getOne(id);
//
//        return R.success("分页查询所有adminStock成功！",adminStock);
//    }
//
//
    /**
     * 添加
     * @param adminStock
     * @return
     */
    @PostMapping("/addAdminStock")
    public R add(@RequestBody AdminStock adminStock){

        LambdaQueryWrapper<AdminStock> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Strings.isNotEmpty(adminStock.getJobNumber()),AdminStock::getJobNumber,adminStock.getJobNumber());
        AdminStock adminStock1=adminStockService.getOne(lqw);

        if (adminStock1==null) {
            Boolean update = adminStockService.save(adminStock);
            if (update) {
                return R.success("添加adminStock成功!", null);
            }
        }
        return R.error("工号已存在");
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @DeleteMapping("/deleteAdminStock/{id}")
    public R deleteById(@PathVariable int id){

        Boolean byId = adminStockService.removeById(id);
        if (byId){
            return R.success("删除adminStocks成功!",null);
        }
        return R.error("删除adminStocks失败!");
    }

    /**
     * 修改
     * @param adminStock
     * @return
     */
    @PutMapping("/updateAdminStock")
    public R deleteById(@RequestBody AdminStock adminStock){

        Boolean update = adminStockService.updateById(adminStock);
        if (update){
            return R.success("修改adminStock成功!",adminStock);
        }
        return R.error("修改adminStock失败!");
    }


}

